<?php

namespace app\admin\model\staff;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Package extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'package';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'state_text',
        'time',
        'city_text',
        'province_text',
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }


    public function getStateList()
    {
        return ['0' => __('State 0'), '1' => __('State 1'), '2' => __('State 2')];
    }

    public function geValueList()
    {
        return ['1' => __('1'), '2' => __('2'), '3' => __('3'), '4' => __('4'), '5' => __('5')];
    }

    public function getCityTextAttr($value, $data)
    {
        $info = Db::name('area')->where(['id' => $data['city']])->find();
        return $info ? $info['name'] : '';
    }

    public function getProvinceTextAttr($value, $data)
    {
        $info = Db::name('area')->where(['id' => $data['province']])->find();
        return $info ? $info['name'] : '';
    }

    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getTimeAttr($value, $data)
    {
        return $data['starttime'].'-'.$data['endtime'];
    }

}
